package com.thoughtworks;
import java.util.Scanner;

public class AccountManager {

    Scanner sc = new Scanner(System.in);
    AccountRepository accountRepository = new AccountRepository();

    public void homeInterface(){
        //Scanner sc = new Scanner(System.in);
        String home = "1. 注册register\n" +
                "2. 登录login\n" +
                "3. 退出quit\n" +
                "请输入你的选择choose(1~3)：";
        System.out.println(home);
        int input = sc.nextInt();
        switch (input){
            case 1:
                register();
                break;
            case 2:
                logIn();
                break;
            case 3:
                quit();
                break;
        }
    }

    public void register(){
        String register = "请输入注册信息(格式：用户名,手机号,邮箱,密码 name, mobile, email, key)：";
        String wrongInput = "格式错误 wrong format\n" +
                "请按正确格式输入注册信息：";
        System.out.println(register);
        String registerInfo = sc.next();
        if(InputConstrain.isCorrectRegister(registerInfo)){
            String result = accountRepository.accountRegister(registerInfo);
            System.out.println(result);
        } else {
            System.out.println(wrongInput);
        }
        homeInterface();
    }

    public void logIn(){
        String logIn = "请输入用户名和密码(格式：用户名,密码, name, key)：";
        String wrongInput = "格式错误wrong input\n" +
                "请按正确格式输入用户名和密码enter correct format please：";
        String threeTimes = "您已3次输错密码，账号被锁定 three times already!";
        System.out.println(logIn);

        int logInTimes = 0;
        int maxTry = 3;
        String succeed = "no";
        while(logInTimes<maxTry&&succeed.equals("no")){
            String logInInfo = sc.next();
            if(InputConstrain.isCorrectLogIn(logInInfo)){
                String returnInfo = accountRepository.accountLogIn(logInInfo);
                System.out.println(returnInfo);
                logInTimes += 1;
                if(!returnInfo.startsWith("密码或用户名错误")){
                    succeed = "yes";
                }
            } else {
                System.out.println(wrongInput);
            }
            if(logInTimes==maxTry){
                System.out.println(threeTimes);
            }
        }
        homeInterface();
    }

    public void quit(){
        String quit = "=================================\n"+"退出用户管理系统quit account manager system";
        System.out.println(quit);
    }
}
